package com.freecat.xmqgct.mvp.ui.activity;

import android.Manifest;
import android.location.Location;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.widget.Toast;

import com.blankj.utilcode.util.NetworkUtils;
import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;


import com.freecat.xmqgct.MainActivity;
import com.freecat.xmqgct.R;
import com.freecat.xmqgct.mvp.presenter.MainPresenter;
import com.freecat.xmqgct.utils.Constant;
import com.freecat.xmqgct.utils.gps.GPSLocationListener;
import com.freecat.xmqgct.utils.gps.GPSLocationManager;
import com.freecat.xmqgct.utils.gps.GPSProviderStatus;
import com.qw.soul.permission.SoulPermission;
import com.qw.soul.permission.bean.Permission;
import com.qw.soul.permission.bean.Permissions;
import com.qw.soul.permission.callbcak.CheckRequestPermissionsListener;

import me.jessyan.art.mvp.IView;
import me.jessyan.art.mvp.Message;
import me.jessyan.art.utils.ArtUtils;
import timber.log.Timber;

import static me.jessyan.art.utils.Preconditions.checkNotNull;


/**
 * ================================================
 * Description:
 * <p>
 * Created by MVPArtTemplate on 12/09/2019 17:59
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * <a href="https://github.com/JessYanCoding/MVPArt">Star me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms/wiki">See me</a>
 * <a href="https://github.com/JessYanCoding/MVPArmsTemplate">模版请保持更新</a>
 * ================================================
 */
public class WelcomeActivity extends BaseActivity<MainPresenter> implements IView {

    private GPSLocationManager locationManager;

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_welcome; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        requestPermission();
//        locationManager = GPSLocationManager.getInstances(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
//        initLocation();
    }

    private void initLocation() {
        locationManager.start(new MyListener(), true);
    }

    @Override
    protected void onPause() {
        super.onPause();
//        locationManager.stop();
    }

    class MyListener implements GPSLocationListener {

        @Override
        public void UpdateLocation(Location location) {
            if (location != null) {
                ToastUtils.showShort("经度：" + location.getLongitude() + "\n纬度：" + location.getLatitude());
                Timber.d("经度：" + location.getLongitude() + "\n纬度：" + location.getLatitude());
            }
        }

        @Override
        public void UpdateStatus(String provider, int status, Bundle extras) {
            if ("gps".equals(provider)) {
                Toast.makeText(WelcomeActivity.this, "定位类型：" + provider, Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void UpdateGPSProviderStatus(int gpsStatus) {
            switch (gpsStatus) {
                case GPSProviderStatus.GPS_ENABLED:
                    Toast.makeText(WelcomeActivity.this, "GPS开启", Toast.LENGTH_SHORT).show();
                    break;
                case GPSProviderStatus.GPS_DISABLED:
                    Toast.makeText(WelcomeActivity.this, "GPS关闭", Toast.LENGTH_SHORT).show();
                    break;
                case GPSProviderStatus.GPS_OUT_OF_SERVICE:
                    Toast.makeText(WelcomeActivity.this, "GPS不可用", Toast.LENGTH_SHORT).show();
                    break;
                case GPSProviderStatus.GPS_TEMPORARILY_UNAVAILABLE:
                    Toast.makeText(WelcomeActivity.this, "GPS暂时不可用", Toast.LENGTH_SHORT).show();
                    break;
                case GPSProviderStatus.GPS_AVAILABLE:
                    Toast.makeText(WelcomeActivity.this, "GPS可用啦", Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    }

    /*
    * 初始化
    * */
    private void requestPermission() {
        Permissions permissions = Permissions.build(Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.READ_PHONE_STATE);
        SoulPermission.getInstance().checkAndRequestPermissions(permissions, new CheckRequestPermissionsListener() {
            @Override
            public void onAllPermissionOk(Permission[] allPermissions) {

                String token = SPUtils.getInstance(Constant.SP_CONFIG).getString(Constant.TOKEN);
                System.out.println("token==================="+token);
                boolean connected = NetworkUtils.isConnected();

                long totalMilliSeconds = System.currentTimeMillis();
                long totalSeconds = totalMilliSeconds / 1000;

                String time_token = SPUtils.getInstance(Constant.SP_CONFIG).getString(Constant.Time_Token);

                if (time_token!=null&&time_token.length()>2){
                    System.out.println("当前时间戳==========="+totalSeconds+"数据库缓存时间戳========="+time_token);

                    Long restime=totalSeconds-Long.valueOf(time_token);
                    if (restime<7200){
                        System.out.println("当前时间戳小于120分钟，直接进入MAIN");
                        ArtUtils.startActivity(MainActivity.class);
                    }else {
                        SPUtils.getInstance(Constant.SP_CONFIG).remove(Constant.Time_Token);
                        System.out.println("当前时间戳大于120分钟，重新登录");
                        ArtUtils.startActivity(AccountLoginActivity.class);
                    }
                }else {
                    System.out.println("没有检测到时间缓存，重新登录");
                    ArtUtils.startActivity(AccountLoginActivity.class);
                }
//                if (token != null) {
//                    if (connected) {
//                        SPUtils.getInstance(Constant.SP_CONFIG).put(Constant.OFFLINE, false);
//                        ArtUtils.startActivity(FaceLivenessExpActivity.class);
//                    } else {
//                        SPUtils.getInstance(Constant.SP_CONFIG).put(Constant.OFFLINE, true);
//                        ArtUtils.startActivity(AccountLoginActivity.class);
//                    }
//                    if (connected) {
//                        SPUtils.getInstance(Constant.SP_CONFIG).put(Constant.OFFLINE, false);
//                        ArtUtils.startActivity(FaceLivenessExpActivity.class);
//                    } else {
//                        SPUtils.getInstance(Constant.SP_CONFIG).put(Constant.OFFLINE, true);
//                        ArtUtils.startActivity(AccountLoginActivity.class);
//                    }
//
//                    SPUtils.getInstance(Constant.SP_CONFIG).put(Constant.OFFLINE, true);
//                    ArtUtils.startActivity(AccountLoginActivity.class);
//                } else {
//
//                    SPUtils.getInstance(Constant.SP_CONFIG).put(Constant.OFFLINE, true);
//                    ArtUtils.startActivity(AccountLoginActivity.class);
////                    ArtUtils.startActivity(FaceLivenessExpActivity.class);
//                }
                finish();
            }

            @Override
            public void onPermissionDenied(Permission[] refusedPermissions) {
                requestPermission();
            }
        });
    }


    @Override
    @Nullable
    public MainPresenter obtainPresenter() {
        return new MainPresenter(ArtUtils.obtainAppComponentFromContext(this));
    }

    @Override
    public void showLoading() {
    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArtUtils.makeText(this, message);
    }

    @Override
    public void handleMessage(@NonNull Message message) {
        checkNotNull(message);
        switch (message.what) {
            case 0:
                break;

        }
    }
}
